<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mreviewcoupon extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
   
    public function getReviewcouponDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql  = '';
        $sql = "SELECT e.id, e.title as etitle, m.id as mid, m.fullname, er.id as erid, er.name as name_re, er.star, er.content, er.improved, er.created FROM ecoupon_review as er LEFT JOIN ecoupon as e ON er.coupon_id = e.id LEFT JOIN member as m ON m.id = er.user_id WHERE 1=1 ";
		
		$keyword = $this->getParamString('keyword');
        if(!in_array($keyword, array(null, ''))){
            $sql .= "AND e.`title` LIKE '%".$keyword."%' ";
        }
		
		//die($sql) ;
        $sql .= "ORDER BY er.created DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        //return $xs;
        //foreach($xs AS $el=>$le)
       // {
         //   $xs[$el]->zone = $this->db->query('SELECT rz.`title`, rz.id FROM restaurant_address AS z INNER JOIN zone AS rz ON rz.id = z.id_zone WHERE z.id = '.$le->id)->result_object();
           // 
           // $xs[$el]->restaurant = $this->db->query('SELECT rck.`name`, rck.id  FROM restaurant_address AS z INNER JOIN restaurant AS rck ON rck.id = z.`restaurant_id` WHERE z.id = '.$le->id)->result_object();
       // }
        
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);   
    }

	public function deleteReviewcouponDb($id){
        $this->db->delete('ecoupon_review', array('id' => $id)); 
        return 200;
	}
	
	public function getReviewcouponInfo($id){
		return $this->db->query("SELECT e.id as eid, e.title as etitle, m.id as mid, m.fullname, er.id as erid, er.name as name_re, er.star, er.content, er.improved, er.created FROM ecoupon_review as er LEFT JOIN ecoupon as e ON er.coupon_id = e.id LEFT JOIN member as m ON m.id = er.user_id WHERE er.id = '".$id."'")->row_object();
	}
	
	public function editReviewcoupon($id,$coupon_id,$user_id,$star,$content,$improved){
		$data = array(
			'coupon_id' => $coupon_id,
			'user_id' => $user_id,
            'star' => $star,
			'content' => $content,
			'improved' => $improved
        );
        $this->db->update('ecoupon_review', $data, "id = ".$id);
        return 200;
	}
	
	public function optionCounpon($id)
    {
        $data = $this->db->query('SELECT id, title FROM ecoupon ORDER BY id ASC')->result_object();
        $html = '<option value = "">Chọn coupon</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }
	
	public function optionMember($id)
    {
        $data = $this->db->query('SELECT id, username,fullname FROM member ')->result_object();
        $html = '<option value = "">Chọn người dùng</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->fullname.'</option>';
        }
        return $html;
    }
	
	public function optionStar($id)
    {
        $html = '<option value = "">Chọn số sao</option>';
        for ( $i = 1; $i <=5; $i++  ) 
        {
            if($i == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$i.'" '.$selected.'>'.$i.' Sao</option>';
        }
        return $html;
    }   
	
}
 